<?php
/**
 * 配送类
 */

include_once FUN_PTH.'delivery.inc.php';

class Delivery extends MyBase{
	
	function __construct() {
		parent::__construct('delivery.id', array('keyword' => array('name', 'desc')));
	}
	
	/**
	 * 修改配送
	 */
	public function edit($post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		if($id <= 0)return false;
		$select = array('desc' => $post['desc'], 'kept' => " AND id!={$id}");
		if($id > 0){
			$select['kept'] = " AND id!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)){return lang('already').lang('exist');}
		$data = array(
			'name'			=> $post['name'],
			'desc'			=> $post['desc'],
			'feemod'		=> $post['feemod'],
			'freeWeight'	=> $post['freeWeight'],
			'firstWeight'	=> $post['firstWeight'],
			'fee'			=> $post['fee'],
			'followWeight'	=> $post['followWeight'],
			'followFee'		=> $post['followFee'],
			'deliveryDate'	=> $post['deliveryDate'],
		);
		if($id > 0){
			$data['updatetime'] = TIME;
			$r = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			$r = $id = $this->db->insert($this->tbname, $data);
		}
		if($r > 0){
			return $this->cache_delivery(); // 缓存配送
		}
		return $r;
	}
	
	public function update(){
		$deliverys = get_deliverys();
		foreach($deliverys['list'] as $mark => $delivery){
			$data = array (
				'mark'			=> $mark,
				'name'			=> $delivery['name'],
				'logo'			=> $delivery['logo'],
				'url'			=> $delivery['url'],
				'desc'			=> $delivery['desc'],
				'feemod'		=> $delivery['feemod'],
				'freeWeight'	=> $delivery['freeWeight'],
				'firstWeight'	=> $delivery['firstWeight'],
				'fee'			=> $delivery['fee'],
				'followWeight'	=> $delivery['followWeight'],
				'followFee'		=> $delivery['followFee'],
				'deliveryDate'	=> $delivery['deliveryDate'],
				'author'		=> $delivery['author'],
				'version'		=> $delivery['version'],
				'pubdate'		=> $delivery['pubdate'],
				'dir'			=> $delivery['dir'],
			);
			$rs = $this->get_row_by_mark($mark, '`id`');
			if(!empty($rs)){
				$data['updatetime'] = TIME;
// 				$r = $this->db->update($this->tbname, array('id' => $rs['id']), $data);
			}else{
				$data['addtime'] = TIME;
				$r = $this->db->insert($this->tbname, $data);
			}
		}
		return $this->cache_delivery(); // 缓存配送
	}
		
	/**
	 * 更新排序
	 */
	public function rank($post){
		if(empty($post['rank'])){return false;}
		if(!is_array($post['rank']))$post['rank'] = array($post['rank']);
		if(!is_array($post['state']))$post['state'] = array($post['state']);
		$iRtn = 0;
		foreach($post['lst'] as $id){
			$data = array(
				'rank'		 => $post['rank'][$id], 
				'updatetime' => TIME 
			);
			if(isset($post['state'][$id])){
				$data['state'] = 1;
			}else{
				$data['state'] = 0;
			}
			if($this->db->update($this->tbname, array('id' => $id), $data)){
				$iRtn ++;
			}
		}
		return $this->cache_delivery();
	}
	
	public function activate($post){
		if(empty($post['state'])){
			$post['state'] = array();
		}elseif(!is_array($post['state'])){
			$post['state'] = array($post['state']);
		}
		$iRtn = 0;
		foreach($post['lst'] as $id){
			$data = array(
				'state'		 => isset($post['state'][$id]) ? 1 : 0, 
				'updatetime' => TIME 
			);
			if($this->db->update($this->tbname, array('id' => $id), $data)){
				$iRtn ++;
			}
		}
		return $this->cache_delivery();
	}
			
	
	/**
	 * 取一条记录
	 */
	/*public function get_row($id, $fields = '*'){
		return $this->db->get_row($this->tbname, array('id' => $id), $fields);
	}*/
	
	public function get_row_by_mark($mark, $fields = '*'){
		return $this->db->get_row($this->tbname, array('mark' => $mark), $fields);
	}
	
	/**
	 * 取列表
	 */
	/*public function get_list($fields = '*', $select = array(), $order = array(), $page = 1, $size = 10, $offset = PAGE_OFFSET){
		$select['kept'] = '';
		if(!empty($select['keyword'])){
			$select['kept'] .= " AND (`name` LIKE '%{$select['keyword']}%' OR `desc` LIKE '%{$select['keyword']}%')";
		}
		return $this->db->get_list($this->tbname, $fields, $select, $order, $page, $size, $offset);
	}*/
	
	/*public function get_all($fields = '*', $select = array(), $order = array()){
		$select['kept'] = '';
		if(!empty($select['keyword'])){
			$select['kept'] .= " AND (`name` LIKE '%{$select['keyword']}%' OR `desc` LIKE '%{$select['keyword']}%')";
		}
		return $this->db->get_all($this->tbname, $fields, $select, $order);
	}*/
	
	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
		
	public function cache_delivery(){
		$list = $this->get_all('*', array('state' => 1), array('rank' => 'DESC', 'id' => 'ASC'));
		if(!empty($list['list'])){
			$deliverys = array();
			foreach($list['list'] as $delivery){
				$deliverys[$delivery['mark']] = $delivery;
			}
			return cache_write('deliverys.php', $deliverys);
		}
		return true;
	}
	
}
?>